package per.javase.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

//使用preparedstatement完成增删改
public class jdbcTest01 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
//            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost/bjpowernode","root","19970913Xun");
            //3.获取数据库操作对象
            //数据库的修改操作
            // String sql = "update emp1 set comm = ? where empno = 8888";
            // ps = conn.prepareStatement(sql);
            // ps.setInt(1,6666);
            //数据库的增加操作
            //String sql = "insert into emp1 values(?,?,?,?,?,?,?,?)";
            //ps = conn.prepareStatement(sql);
            //ps.setInt(1,1234);
            //ps.setString(2,"Tony");
            //ps.setString(3,"doctor");
            //ps.setInt(4,9999);
            //ps.setString(5,"1990-5-31");
            //ps.setInt(6,4000);
            //ps.setInt(7,1000);
            //ps.setInt(8,20);
            //数据库的删除操作
            String sql = "delete from emp1 where empno = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,1234);
            //4.执行SQl语句
            int count = ps.executeUpdate();
            System.out.println(count ==1?"修改成功！":"修改失败");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            //6.释放资源
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
        }
    }
}
